﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web.UI;
using Ornament.MemberShip;
using Ornament.MemberShip.Module.Presenters;
using Ornament.MemberShip.Module.Views;

public partial class Admin_MemberAdmin_Controls_UserGroupAssign : UserControl, IUserGroupAssignView
{
    #region IUserGroupAssignView Members

    public string CurrentUserId
    {
        get { return ViewState["currentUserId"] == null ? null : ViewState["currentUserId"].ToString(); }
        set { ViewState["currentUserId"] = value; }
    }

    public Guid[] SelectedUserGroupIds
    {
        get
        {
            string[] ids = HiddenBoxAssignUserGroupId.Value.Split(',');
            var result = new List<Guid>();
            foreach (string id in ids)
            {
                if (!String.IsNullOrEmpty(id))
                {
                    result.Add(new Guid(id));
                }
            }
            return result.ToArray();
        }
    }

    public void BindNotAssignUserGroup(ReadOnlyCollection<UserGroup> groups)
    {
        NoAssignGroupListBox.DataSource = groups;
        NoAssignGroupListBox.DataBind();
    }

    public void BindAssignUserGroup(ReadOnlyCollection<UserGroup> grousp)
    {
        AssignGroupListBox.DataSource = grousp;
        AssignGroupListBox.DataBind();
    }

    public UserGroupAssignPresenter Presenter { get; set; }

    public void AssignUserGroupTo(User user)
    {
        if (user == null) throw new ArgumentNullException("user");

        Presenter.UpdateWith(user);
    }

    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        Presenter = new UserGroupAssignPresenter(this);
        if (!IsPostBack)
        {
            Presenter.InitilizeView();
        }
        Presenter.ViewLoad();
    }
}